/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "Precompiled.h"

#include "Selector.h"
#include "Context.h"
#include "Program.h"
#include "Variable.h"

using namespace System;

namespace OptixDotNet 
{
	Selector::Selector( Context^ context ) : ContainerNode( context )
	{
		mSelector = 0;

		pin_ptr<RTselector> ptr = &mSelector;
		CheckError( rtSelectorCreate( context->InternalPtr(), ptr ) );

		mCollection = gcnew NodeCollection<ContainerNode^>( this );
	}	

	Selector::Selector( Context^ context, RTselector selector ) : ContainerNode( context )
	{
		mSelector = selector;
		mCollection = gcnew NodeCollection<ContainerNode^>( this );
	}

	ContainerNode^ Selector::ChildNodes::get( unsigned int index )
	{
		return GetChild( index );
	}

	void Selector::ChildNodes::set( unsigned int index, ContainerNode^ node )
	{
		SetChild( index, node );
	}

	ContainerNode^ Selector::GetChild( unsigned int index )
	{
		if( index >= ChildCount )
		{
			throw gcnew ArgumentOutOfRangeException( "index" );
		}

		RTobject child;
		CheckError( rtSelectorGetChild( mSelector, index, &child ) );

		RTobjecttype type;
		CheckError( rtSelectorGetChildType( mSelector, index, &type ) );

		return ConstructContainerNodeFromType( (Type)type, child );
	}

	void Selector::SetChild( unsigned int index, ContainerNode^ node )
	{
		CheckError( rtSelectorSetChild( mSelector, index, node->ObjectPtr() ) );
	}

	Program^ Selector::VisitProgram::get()
	{
		RTprogram program;
		CheckError( rtSelectorGetVisitProgram( mSelector, &program ) );

		return gcnew Program( mContext, program );
	}

	void Selector::VisitProgram::set( Program^ program )
	{
		CheckError( rtSelectorSetVisitProgram( mSelector, program->InternalPtr() ) );
	}

	Variable^ Selector::default::get( String^ s )
	{
		if( String::IsNullOrEmpty( s ) )
			throw gcnew Exception( "Selector Error: Variable name is null or empty" );

		const char* str = (const char*)StrToAnsii( s );
		
		RTvariable rtVar;
		CheckError( rtSelectorQueryVariable( mSelector, str, &rtVar ) );

		if( rtVar == 0 ) 
			CheckError( rtSelectorDeclareVariable( mSelector, str, &rtVar ) );

		return gcnew Variable( mContext, rtVar );
	}

	void Selector::default::set( String^ s, Variable^ var )
	{
		if( String::IsNullOrEmpty( s ) )
			throw gcnew Exception( "Selector Error: Variable name is null or empty" );

		const char* str = (const char*)StrToAnsii( s );

		RTvariable rtVar;
		CheckError( rtSelectorQueryVariable( mSelector, str, &rtVar ) );

		if( rtVar != 0 && var == nullptr )
		{
			CheckError( rtSelectorRemoveVariable( mSelector, rtVar ) );
		}
		else
		{
			throw gcnew Exception( "Selector Error: Variable copying not yet implemented" );
		}
	}

	Variable^ Selector::default::get( int index )
	{
		if( index < 0 || index >= VariableCount )
			throw gcnew ArgumentOutOfRangeException( "index" );
		
		RTvariable rtVar;
		CheckError( rtSelectorGetVariable( mSelector, index, &rtVar ) );

		return gcnew Variable( mContext, rtVar );
	}

	void Selector::default::set( int index, Variable^ var )
	{
		if( index < 0 || index >= VariableCount )
			throw gcnew ArgumentOutOfRangeException( "index" );

		RTvariable rtVar;
		CheckError( rtSelectorGetVariable( mSelector, index, &rtVar ) );

		if( var == nullptr )
		{
			CheckError( rtSelectorRemoveVariable( mSelector, rtVar ) );
		}
		else
		{
			throw gcnew Exception( "Selector Error: Variable copying not yet implemented" );
		}
	}
}